﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmTdTc78 : Form
    {
        public FrmTdTc78()
        {
            InitializeComponent();
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab==tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Tdtc78dau;
            }
            else
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Tdtc78san;
            }
        }

        private void FrmTdTc78_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmTdTc78_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(556, 560);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrTdTc78 prTdTc78 = new PrTdTc78();
            prTdTc78.Show();
            this.Hide();
        }

        private void FrmTdTc78_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }

             // Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            Values = txtCaotuongcanhH2 .Text;//0
            Values = Values +"\t"+ txtChankhayb3 .Text;//1
            Values = Values +"\t"+ txtChankhayH3 .Text;//2
            Values = Values +"\t"+ txtChankhayb2 .Text;//3
            Values = Values +"\t"+ txtChankhayH2 .Text;//4
            Values = Values +"\t"+ txtCaotaluyh1 .Text;//5
            Values = Values +"\t"+ txtCaotaluyh2 .Text;//6
            Values = Values +"\t"+ txtCaotuongdauH1 .Text;//7
            Values = Values +"\t"+ txtDaymongH4 .Text;//8
            Values = Values +"\t"+ txtDaysanH1 .Text;//9
            Values = Values +"\t"+ txtDaytuong .Text;//10
            Values = Values +"\t"+ txtGiacoL .Text;//11
            Values = Values +"\t"+ txtGocmoa1 .Text;//12
            Values = Values +"\t"+ txtMorongb1 .Text;//13
            Values = Values +"\t"+ txtMorongb2 .Text;//14
            Values = Values +"\t"+ txtMorongmongb5 .Text;//15
            Values = Values +"\t"+ txtLopdem .Text;//16
            Values = Values +"\t"+ txtDoctuongi .Text;//17
            Values = Values +"\t"+ txtNghiengMong .Text;//18
            //Gioithieu
            Values = Values +"\t"+ txtMorongb0 .Text;//19
            Values = Values +"\t"+ txtDaymongH5 .Text;//20
            Values = Values +"\t"+ optRongsangiaco.Checked.ToString();//21
            Values = Values +"\t"+ txtMorongsan .Text;//22
            regKey.SetValue("KtTdTc78", Values);
            regKey.Close();

        }
          // Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtTdTc78").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                txtCaotuongcanhH2 .Text=STR[i++];//0
                txtChankhayb3.Text = STR[i++];//1
                txtChankhayH3.Text = STR[i++];//2
                txtChankhayb2.Text = STR[i++];//3
                txtChankhayH2.Text = STR[i++];//4
                txtCaotaluyh1.Text = STR[i++];//5
                txtCaotaluyh2.Text = STR[i++];//6
                txtCaotuongdauH1.Text = STR[i++];//7
                txtDaymongH4.Text = STR[i++];//8
                txtDaysanH1.Text = STR[i++];//9
                txtDaytuong.Text = STR[i++];//10
                txtGiacoL.Text = STR[i++];//11
                txtGocmoa1.Text = STR[i++];//12
                txtMorongb1.Text = STR[i++];//13
                txtMorongb2.Text = STR[i++];//14
                txtMorongmongb5.Text = STR[i++];//15
                txtLopdem.Text = STR[i++];//16
                txtDoctuongi.Text = STR[i++];//17
                txtNghiengMong.Text = STR[i++];//18
                //Gioithieu
                txtMorongb0.Text = STR[i++];//19
                txtDaymongH5.Text = STR[i++];//20
                optRongsangiaco.Checked=bool.Parse(STR[i++]);//21
                txtMorongsan.Text = STR[i++];//22
                regKey.Close();
            }
        }
         // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.duvr)|*.duvr";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Tuong canh
                Str = "------TUONG CANH------" +"\r\n";
                Str = Str + "Cao tuong H1:" +"\t"+ txtCaotuongdauH1 .Text+"\r\n";
                Str = Str + "Cao tuong H2:" +"\t"+ txtCaotuongcanhH2 .Text+"\r\n";
                Str = Str + "Sau chan khay H3:" +"\t"+ txtChankhayH3 .Text+"\r\n";
                Str = Str + "Day mong cong H4:" +"\t"+ txtDaymongH4 .Text+"\r\n";
                Str = Str + "Day mong cong H5:" +"\t"+ txtDaymongH5 .Text+"\r\n";
                Str = Str + "Mo rong mong b1:" +"\t"+ txtMorongb1 .Text+"\r\n";
                Str = Str + "Mo rong mong b1:" +"\t"+ txtMorongb0 .Text+"\r\n";
                Str = Str + "Dai cuoi san b2:" +"\t"+ txtMorongb2 .Text+"\r\n";
                Str = Str + "Day chan khay b3:" +"\t"+ txtChankhayb3 .Text+"\r\n";
                Str = Str + "Day dinh tuong b4:" +"\t"+ txtDaytuong .Text+"\r\n";
                Str = Str + "Mo rong mong b5:" +"\t"+ txtMorongmongb5 .Text+"\r\n";
                Str = Str + "Doc taluy tuong m:" +"\t"+ txtDoctuongm .Text+"\r\n";
                Str = Str + "Doc lung tuong i:" +"\t"+ txtDoctuongi .Text+"\r\n";
                Str = Str + "Goc mo tren:" +"\t"+ txtGocmoa1 .Text+"\r\n";
                Str = Str + "Goc mo duoi:" +"\t"+ txtGocmoa2 .Text+"\r\n";
                //Gia co
                Str = Str + "------GIA CO------" +"\r\n";
                Str = Str + "Day san gia co H1:" +"\t"+ txtDaysanH1 .Text+"\r\n";
                Str = Str + "Nghieng chan khay i:" +"\t"+ txtNghiengMong .Text+"\r\n";
                Str = Str + "Sau chan khay H2:" +"\t"+ txtChankhayH2 .Text+"\r\n";
                Str = Str + "Day chan khay b2:" +"\t"+ txtChankhayb2 .Text+"\r\n";
                Str = Str + "Lop dem lot H3:" +"\t"+ txtLopdem .Text+"\r\n";
                Str = Str + "Cao dinh op mai H4:" +"\t"+ txtCaotaluyh1 .Text+"\r\n";
                Str = Str + "Cao chan op mai H5:" +"\t"+ txtCaotaluyh2 .Text+"\r\n";
                Str = Str + "Dai san gia co b1:" +"\t"+ txtGiacoL .Text+"\r\n";
                Str = Str + "Rong dinh op mai b3:" +"\t"+ txtRongtaluyB1 .Text+"\r\n";
                Str = Str + "Rong san gia co b4:" +"\t"+ txtRongsangiacob4 .Text+"\r\n";
                Str = Str + "Mo rong san :" +"\t"+ txtMorongsan .Text+"\r\n";
                Str = Str + "Mo rong san gia co:" +"\t"+ optMorongsangiaco.Checked.ToString()+"\r\n";
                Str = Str + "Rong san gia co:" +"\t"+ optRongsangiaco.Checked.ToString()+"\r\n";
                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }
               // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.duvr)|*.duvr";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Tuong canh
                txtCaotuongdauH1 .Text=Values[i].Split('\t')[1]; i++;
                txtCaotuongcanhH2 .Text=Values[i].Split('\t')[1]; i++;
                txtChankhayH3 .Text=Values[i].Split('\t')[1]; i++;
                txtDaymongH4 .Text=Values[i].Split('\t')[1]; i++;
                txtDaymongH5 .Text=Values[i].Split('\t')[1]; i++;
                txtMorongb1 .Text=Values[i].Split('\t')[1]; i++;
                txtMorongb0 .Text=Values[i].Split('\t')[1]; i++;
                txtMorongb2 .Text=Values[i].Split('\t')[1]; i++;
                txtChankhayb3 .Text=Values[i].Split('\t')[1]; i++;
                txtDaytuong .Text=Values[i].Split('\t')[1]; i++;
                txtMorongmongb5 .Text=Values[i].Split('\t')[1]; i++;
                txtDoctuongm .Text=Values[i].Split('\t')[1]; i++;
                txtDoctuongi .Text=Values[i].Split('\t')[1]; i++;
                txtGocmoa1 .Text=Values[i].Split('\t')[1]; i++;
                txtGocmoa2 .Text=Values[i].Split('\t')[1]; i++;
                //Gia co
                i = i + 1;
                txtDaysanH1 .Text=Values[i].Split('\t')[1]; i++;
                txtNghiengMong .Text=Values[i].Split('\t')[1]; i++;
                txtChankhayH2 .Text=Values[i].Split('\t')[1]; i++;
                txtChankhayb2 .Text=Values[i].Split('\t')[1]; i++;
                txtLopdem .Text=Values[i].Split('\t')[1]; i++;
                txtCaotaluyh1 .Text=Values[i].Split('\t')[1]; i++;
                txtCaotaluyh2 .Text=Values[i].Split('\t')[1]; i++;
                txtGiacoL .Text=Values[i].Split('\t')[1]; i++;
                txtRongtaluyB1 .Text=Values[i].Split('\t')[1]; i++;
                txtRongsangiacob4 .Text=Values[i].Split('\t')[1]; i++;
                txtMorongsan .Text=Values[i].Split('\t')[1]; i++;
                optMorongsangiaco.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                optRongsangiaco.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmTdTc78_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
